<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\response\Json;

class CadreKinship extends Controller
{
    /**
     * 统计查询
     *
     * @return \think\Response
     */
    public function index()
    {
        $name=input('name');
        $name='县民政局';
//        单位分类
        $unit_classification = Db::name('Department')->where('pid','eq',0)->field('id,pid,name')->select();
        foreach($unit_classification as $k => $value){
            $unit_classification[$k]['unit']= Db::name('Department')->where('pid','eq',$value['id'])->field('id,pid,name')->select();
        }
//        干部亲属占比
        $total=Db::name('Cadre_relative')->where('company_id','eq',$name)->count();
        $unit_nums=Db::name('Cadre_relative')->where('relationship','eq','本人')->where('company_id','eq',$name)->count();
        $relatives_nums=Db::name('Cadre_relative')->where('relationship','neq','本人')->where('company_id','eq',$name)->count();
        $proportion_unit='0.00%';
        $proportion_relatives='0.00%';
        if ($name) {
            $proportion_unit=(sprintf("%.4f",$unit_nums/$total))*100 .'%';
            $proportion_relatives=(sprintf("%.4f",$relatives_nums/$total))*100 .'%';
        }
        $proportion[]=array('干部人数'=>$unit_nums,'亲属人数'=>$relatives_nums,'干部比例'=>$proportion_unit,'亲属比例'=>$proportion_relatives);
//        干部
        $unit=Db::name('Cadre_relative')->where(['relationship'=>'本人','company_id'=>$name])->select();
        foreach($unit as $k => $value){
            $unit[$k]['relatives_nums']=Db::name('Cadre_relative')->where('sfid','eq',$value['sfid'])->where('relationship','neq','本人')->count();
//            干部资金
            $unit_capital[$k]['capital']=Db::name('Income')->where('sfid','eq',$value['sfid'])->count();
            $relatives_unit[$k]=Db::name('Cadre_relative')->where('relationship','neq','本人')->where('sfid','eq',$value['sfid'])->field('q_sfid')->select();

        }
        $datas['status']=1;
        $datas['info']=$unit_classification;
        $datas['proportion']=$proportion;
        return json($datas);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
